package com.shop.manager.controller;

//import com.shop.manager.entity.TbAttribute;
//import com.shop.manager.entity.TbTypeTemplate;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.TbAttribute;
import com.base.common.entity.TbTypeTemplate;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.TypeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 类型模板增删改查
 */
@RestController
@RequestMapping("/template")
public class TypeTemplateController {

    /**
     * @Description 注入service对象
     */
    @Autowired
    private TypeTemplateService typeTemplateService;

    /**
     * @Description 添加类型模板
     * @param typeTemplate 类型模板对象
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    HttpResult add(@RequestBody(required = false) TbTypeTemplate typeTemplate) {
        try {
            typeTemplateService.add(typeTemplate);
            return HttpResult.ok("添加类型模板成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加类型模板失败!");
    }

    /**
     * @Description 批量删除类型模板
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    public HttpResult delete(@RequestBody(required = false) Long[] ids) {
        try {
            typeTemplateService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改类型模板
     * @param typeTemplate 类型模板
     * @return
     */
    @RequestMapping("/update")
    HttpResult update(@RequestBody(required = false) TbTypeTemplate typeTemplate) {
        try {
            typeTemplateService.update(typeTemplate);
            return HttpResult.ok("修改修改类型模板成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改修改类型模板失败!");
    }

    /**
     * @Description 根据ID查询类型模板
     * @param id 类型模板id
     * @return
     */
    @RequestMapping("/getById")
    public HttpResult getById(@RequestParam("id") Long id) {
        return HttpResult.ok(typeTemplateService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param typeTemplate 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) TbTypeTemplate typeTemplate) {
        try {
            //分页查询
            PageResult result = typeTemplateService.getAllTemplate(pageNum, size, typeTemplate);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 模糊查询类型模板
     * @param tbTypeTemplate 类型模板的对象
     * @return
     */
    @RequestMapping("/findByWhere")
    public HttpResult findByWhere(@RequestBody(required = false) TbTypeTemplate tbTypeTemplate) {
        return HttpResult.ok(typeTemplateService.findByWhere(tbTypeTemplate));
    }

    /**
     * @Description 查询全部规格
     * @return
     */
    @RequestMapping("/findAll")
    public HttpResult findAll() {
        return HttpResult.ok(typeTemplateService.findAll());
    }

    /**
     * @Description 获取全部属性不分页
     * @return
     */
    @RequestMapping("/queryAllAttrNotPage")
    public List<TbAttribute> getAllAttrNotPage(){
        return typeTemplateService.getAllAttrNotPage();
    }

    /**
     * @Description 根据模板ID查询规格数据
     * @param typeId
     * @return
     */
    @RequestMapping("/findSpecOptionsList")
    public HttpResult findSpecOptionsList(@RequestParam("typeId") Long typeId) {
        return HttpResult.ok(typeTemplateService.findSpecOptionsList(typeId));
    }

}
